package com.ihr360.payroll.model.dto.salary.meta;

import com.ihr360.authority.sharing.entityhandler.UserResourceHandler;
import com.ihr360.commons.entityhandler.DateHandler;
import com.ihr360.commons.entityhandler.Ihr360ContextHandler;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.payroll.model.vo.PayrollConfigVo;
import com.ihr360.payroll.model.vo.SalaryFieldVo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.util.ArrayList;
import java.util.List;


/**
 * 工资表配置，HR用
 */
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"companyId", "name"}))
@EntityListeners(value = {UserResourceHandler.class, Ihr360ContextHandler.class, DateHandler.class})
public class PayrollConfig extends AbstractPayrollConfig{
    private static final long serialVersionUID = 7951441807220635895L;
    public static final class Field{
        public final static String ID = "id";
        public final static String IS_SYSTEM_TEMPLATE = "isSystemTemplate";
        public final static String NAME = "name";

    }

    @Column(length =40, nullable = false, name = "companyId")
    private String companyId;

    /**
     * 系统模板
     */
    private boolean isSystemTemplate = false;


    public List<SalaryFieldVo> getFieldVos() {
        List<SalaryFieldVo> salaryFieldVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(getFields())) {
            getFields().forEach(field -> {
                PayrollConfigVo payrollConfig = null;
                if (field.getConfig() != null) {
                    PayrollConfig config = field.getConfig();
                    payrollConfig = new PayrollConfigVo(config.getId(), config.getName(), new ArrayList<>(),config.getSalaryAdjustFormula());
                }
                salaryFieldVoList.add(new SalaryFieldVo(field.getId(), field.getCode(), payrollConfig, field.getMeta(), field.getSource(),
                        field.getValueMeta(), field.getName(), field.getType(), field.getTaxType(), field.isTaxMergeField(), field.getPersonalSalaryNumOp(),
                        field.getCompanyCostNumOp()));
            });
        }
        return salaryFieldVoList;
    }



}
